

require('./common');
var gs3 = require('gs3');
var Database = gs3.Models.Database;
var Entity = gs3.Models.Entity;
var config = {
	host: "localhost",
	database: "eforms",
	user: "root",
	password: "password"
};
var db = new Database(config, true);

exports.loadList = function(req, res){
	var rCount=0;
        var company_id = '0';
        var output = "";
        
	db.selectByKey('tborgchart', '*', 'company_id', company_id, function(err, data){
          if(err){
            console.log(err)
          }else{
            output = '<html><head></head><body  style="background-color:#fafafa"><h4>Home / Settings /</h4><h2><img src="/images/ui/workflow.png" style="vertical-align:middle" style="border:0px">&nbsp;Organization Chart</h2>'+
              '<table cellpadding=0 cellspacing=0 width="98%">';
            output += '<tr style="background-color:#f4f4f4">';
            output += '<td colspan=2><a href="/orgchart" id="newOrgchart" class="bluebutton">New Orgchart</a></td>';
            output += '<td align="right"><input type="text" value="Search..." style="width:200px;font-style:Italic;color:#777" /></td>';
            output += '</tr>';
            output += '<tr>';
            output += '<td colspan=3><hr></td>';
            output += '</tr>';
            output += '<tr>';
            output += '<td colspan=3>';
            for(var a=0;a<data.length;a++){
              var ga = new getActive(data[a].status,data[a].id);
              output +=  '<div class="requestview">';
              output += '<div class="requestBox" style="">';
              output += '<h4 style="display: inline;"><img src="/images/ui/workflow.png" style="vertical-align:middle"/>&nbsp;'+ data[a].title +'</h4><br/><br/>';
              output += '<font color="#777" style="font-weight:normal"> ' +  data[a].description + '</font>';
              output += '<div style="float:right"><a href="/orgchart/' +  data[a].id + '"><img src="/images/ui/edit.png" style="vertical-align:middle"/></a>&nbsp;';
              output += '<a href="#"><img src="/images/ui/delete.png" style="vertical-align:middle"/></a>&nbsp;'+ga.updateStatusButton+'</div>';
              output += '</div><br style="clear:both;"/>';
              output += '</div><br/>';
              rCount++;
            }
            if(rCount==0){
              output += '<tr style="background-color:#f2f0bf">';
              output +=  '<td colspan=3 style="border:1px Solid #808a10;padding:10px" valign="top">No record found.</td>';
              output +=  '</tr>';
            }
            output += '</td>';
            output += '</tr>';
            output += '</table>';
            output += '</body></html>';
          }
          res.writeHead(200, {'Content-Type': 'text/html'});
	  res.end(output);
        })
	
}

function getActive(status,id){
  if(status==1){
    this.updateStatusButton = '<a style="cursor:pointer"><img src="/stylesheets/aaron/globe-active.png" style="vertical-align:middle" width="20" onclick="updateOrgchartStatus(0,'+id+')"/></a>';
  }else{
    this.updateStatusButton = '<a style="cursor:pointer"><img src="/stylesheets/aaron/globe-inactive.png" style="vertical-align:middle" width="20" onclick="updateOrgchartStatus(1,'+id+')"/></a>';
  }
}

exports.loadObjects = function(req, res){
  var orgchart_id = req.params.id;
  var nodes = "";lines = "";
  //get nodes
  db.selectByKey('tborgchartobjects', '*', 'orgChart_id', orgchart_id, function(err, data){
    if(err){
      
    }else{
      var getMax = null;
      var objectIndex = null;
      for(var a=0;a<data.length;a++){
        nodes+=data[a].object_id+"++"+data[a].tops+"++"+data[a].lefts+"++"+data[a].heights+"++"+data[a].widths+"++"+data[a].department+"{}";
        objectIndex = parseInt(data[a].object_id.split("_")[1]);
        if(getMax<objectIndex){
          getMax = objectIndex;
        }
      }
      //get lines
      db.selectByKey('tborgchartline', '*', 'orgChart_id', orgchart_id, function(err, datas){
        if(err){
          
        }else{
          for(var a=0;a<datas.length;a++){
            lines+=datas[a].lineClass+"++"+datas[a].parent+"++"+datas[a].child+"++"+datas[a].line_top+"++"+datas[a].line_left+"++"+datas[a].line_height+"++"+datas[a].line_width+"{}";
          }
        }
        res.render("orgchart",{
                title: "Organization Chart",
                nodes:nodes,
                lines:lines,
                startCtr: getMax+1
        });
      })
    }
  })
}